<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 第一行 -->
      <el-row>
        <el-input
          placeholder="请输入内容"
          v-model="queryInfo.query"
          class="input-with-select"
          clearable
          @clear="clearInput"
        >
          <el-button
            slot="append"
            icon="el-icon-location"
            @click="searchInput"
          ></el-button>
        </el-input>
        <el-button type="primary" @click="addGoods">添加商品</el-button>
      </el-row>
      <!-- 商品表格 -->
      <el-row>
        <el-table :data="tableData" border stripe>
          <el-table-column type="index" label="#"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="800px">
          </el-table-column>
          <el-table-column prop="goods_price" label="商品价格（元）">
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间">
            <template slot-scope="scope">
              {{ scope.row.add_time | format }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot:default="scope">
              <!-- 编辑按钮 -->
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="small"
              ></el-button>
              <el-button
                type="warning"
                icon="el-icon-delete"
                size="small"
                @click="deleteGoods(scope.row)"
              >
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10, 50]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据
      tableData: [],
      // 请求参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },

      // 总数据条数
      total: 0,
    };
  },
  methods: {
    // 请求表单数据
    async getData() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      this.$message.success(res.meta.msg);
      this.tableData = res.data.goods;
      this.total = res.data.total;

      console.log(res);
      console.log(this.total);
    },

    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pagesize = newSize;
      this.getData();
    },

    handleCurrentChange(newNum) {
      console.log(newNum);
      this.queryInfo.pagenum = newNum;
      this.getData();
    },

    async searchInput() {
      // const { data: res } = await this.$http.get("goods", {
      //   params: this.queryInfo,
      // });
      // if (res.meta.status != 200) {
      //   return this.$message.error(res.meta.msg);
      // }
      // this.$message.success(res.meta.msg);
      // this.tableData = res.data.goods;
      // this.total = res.data.total;
      // this.getData();
      // console.log(res);
      this.getData();
    },

    clearInput() {
      this.queryInfo.query = "";
      this.getData();
    },

    async deleteGoods(role) {
      const ress = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (ress != "confirm") {
        return this.$message.info("取消删除");
      }

      const { data: res } = await this.$http.delete(`goods/${role.goods_id}`);
      if (res.meta.status != 200) {
        return this.$message.error(res.meta.msg);
      }
      this.$message.success(res.meta.msg);
      this.getData();
      console.log(res);
      console.log(ress);
    },

    addGoods() {
      this.$router.push("goods/add");
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang='less' scoped>
.el-input {
  width: 400px;
}
.el-button {
  margin-left: 20px;
}
</style>